package org.gary.feedback.action;

import static lengreen.util.constant.Constant.DateFomaterString.YYYY_MM_DD_HH_MM_SS;
import static org.gary.core.util.Utils.makeDateFormateJsonConfig;
import static org.gary.core.util.Utils.parseIndex;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.gary.core.service.InitService;
import org.gary.core.util.Utils;
import org.gary.feedback.model.Feedback;
import org.gary.feedback.service.FeedBackService;
import org.gary.product.model.Product;
import org.gary.product.service.ProductService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 留言Action
 * 
 * @author Gary
 */
@Component
@Scope("prototype")
public class FeedBackAction extends ActionSupport implements
		ServletResponseAware, SessionAware, ServletRequestAware {

	private static final long serialVersionUID = 4250591942144856559L;

	private FeedBackService feedBackService;
	private String fbJsonString;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private Map<String, Object> session;
	private InitService initService;
	private ProductService productService;
	
	@Resource
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	private Feedback feedback;
	private List<Feedback> feedbackList;
	/**是否可见*/
	private String visible;
	/**分页开始页*/
	private int start;
	/**分页最大页*/
	private int limit;
	/**删除模式*/
	private String mode;
	/**开始时间*/
	private String fromDate;
	/**结束时间*/
	private String toDate;
	/**留言内容*/
	private String content;
	/**邮箱*/
	private String email;
	/**返回结果字符串*/
	private String result;
	
	/**
	 * 列出留言列表
	 * @throws IOException
	 */
	public void list() throws IOException {
		try {
			start = parseIndex(start, limit);
			Map<String, Object> where = new HashMap<String, Object>();
			where.put(" entity.visible = ", new Boolean(getVisible()));
			setFeedbackList(feedBackService.list(Feedback.class, getStart(),
					getLimit(), where, new Object[] { " entity.id desc " }));
	
			for (Feedback c : feedbackList) {
				System.out.println("fff-->" + c);
			}
	
			JsonConfig config = makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS);
			config.setExcludes(new String[] { "visible" });
			JSONArray feedbackListJsonArray = JSONArray.fromObject(feedbackList, config);
			JSONObject feedbackJsonObject = new JSONObject();
			feedbackJsonObject.put("feedbackTotal", feedBackService
					.selectTotalCount(null));
			feedbackJsonObject.put("feedbackList", feedbackListJsonArray);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(feedbackJsonObject.toString());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 搜索
	 * @throws Exception
	 */
	public void search() throws Exception {
		start = parseIndex(start, limit);
		Map<String, Object> where = new HashMap<String, Object>();
		where.put(" entity.visible = ", new Boolean(visible));
		System.out.println("fromDate-->" + getFromDate());
		System.out.println("toDate-->" + getToDate());
		if (!"".equals(getFromDate().trim())) {
			if ("".equals(getToDate().trim())) {
				where.put(" entity.createDate between ", getFromDate()
						+ " 00:00:00' and '"
						+ Utils.getFormatDate(new Date(), YYYY_MM_DD_HH_MM_SS));
			} else {
				where.put(" entity.createDate between ", getFromDate()
						+ " 00:00:00' and '" + getToDate() + " 23:59:59");
			}
		}
		if(!"".equalsIgnoreCase(getContent())) {
			where.put(" entity.content like ", getContent());
		}
		if(!"".equalsIgnoreCase(getEmail())) {
			where.put(" entity.email like ", getEmail());
		}
		
		setFeedbackList(feedBackService.list(Feedback.class, getStart(), getLimit(), where,
				new Object[] { " entity.id desc " }));

		JsonConfig config = makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS);
		config.setExcludes(new String[] { "visible" });
		JSONArray feedbackListJsonArray = JSONArray.fromObject(feedbackList, config);
		JSONObject feedbackJsonObject = new JSONObject();
		feedbackJsonObject.put("feedbackTotal", feedBackService.selectTotalCount(null));
		feedbackJsonObject.put("feedbackList", feedbackListJsonArray);
		System.out.println("feedbackJsonObject-->" + feedbackJsonObject.toString());
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(feedbackJsonObject.toString());
	}
	
	/**
	 * 删除
	 * @throws IOException
	 */
	public void delete() throws IOException {
		if ("delete".equals(getMode())) {
			feedBackService.delete(feedback);
			result = "{'result':'delete'}";
		} else if ("update".equals(getMode())) {
			feedBackService.update(Feedback.class, new Object[] { feedback
					.getId() }, false);
			result = "{'result':'update'}";
		} else {
			result = "{'result':'none'}";
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(result);
	}
	
	/**
	 * 回收
	 * @throws IOException
	 */
	public void recover() throws IOException {
		try {
			if (feedback != null) {
				feedBackService.update(Feedback.class, new Object[] { feedback
						.getId() }, true);
				result = "{'success':'true'}";
			} else {
				result = "{'success':'false'}";
			}
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Resource
	public void setInitService(InitService initService) {
		this.initService = initService;
	}

	public String getFbJsonString() {
		return fbJsonString;
	}

	public void setFbJsonString(String fbJsonString) {
		this.fbJsonString = fbJsonString;
	}

	@Resource
	public void setFeedBackService(FeedBackService feedBackService) {
		this.feedBackService = feedBackService;
	}

	public void message() throws Exception {
		
		List<Product> banner=productService.getProductList(10,0,session.get("WW_TRANS_I18N_LOCALE"),-1);
		request.setAttribute("banner", banner);
		
		JSONObject fbJsonObject = JSONObject.fromObject(fbJsonString);
		Feedback fb = (Feedback) JSONObject
				.toBean(fbJsonObject, Feedback.class);
		Map<String, Object> session = ActionContext.getContext().getSession();
		String yzm = session.get("piccode").toString();
		if (!fb.getCode().equalsIgnoreCase(yzm)) {
			response.getWriter().write("0");
		} else {
			feedBackService.saveFeedBack(fb);
			response.getWriter().write("1");
		}
	}

	public String show() {
		initService.initAll(session);
		List<Product> banner=productService.getProductList(10,0,session.get("WW_TRANS_I18N_LOCALE"),-1);
		request.setAttribute("banner", banner);
		return SUCCESS;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setVisible(String visible) {
		this.visible = visible;
	}

	public String getVisible() {
		return visible;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}

	public Feedback getFeedback() {
		return feedback;
	}

	public void setFeedbackList(List<Feedback> feedbackList) {
		this.feedbackList = feedbackList;
	}

	public List<Feedback> getFeedbackList() {
		return feedbackList;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getMode() {
		return mode;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
}
